package org.testing;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

import org.testing.model.Test;
import org.testing.model.results.Result;

public class Testing {
	private static final int QUESTIONS_FOR_TEST = 10;
	private static User commonUser;
	
	
	public static void main(String[] args) throws IOException {
		System.out.println("Hello! It is the testing System!");
		
		while(true) {
			System.out.println("Please, enter your name");
			Scanner rd_cmd = new Scanner(System.in);
			String login = rd_cmd.next();
			commonUser = LoginSystem.userLogin(login);
			if (commonUser == null) {
				System.out.println("You not registered, your name is " + login + " do you want to sing up?");
				System.out.println("\"r\" to register or \"n\" for no ");
				String input = rd_cmd.next();
				if (input.equals("r")) {
					LoginSystem.singUp(login);
					System.out.println("Thank you for registering");
					commonUser = LoginSystem.userLogin(login);
				}
				if (input.equals("e")) {
					continue;
				}
					
			}
			FileLogger logger = new FileLogger("log");
			logger.logLogin(commonUser);
			
			while(true) {
				System.out.print("you logged as " + commonUser.getName());
				if (commonUser.IsAdmin()) {
					System.out.println(", admin");
				} else {
					System.out.println();
				}
				System.out.println("s - to view statistics");
				System.out.println("t - to start new test");
				System.out.println("logout - to log out from system");
				System.out.println("e - exit from program");
				
				String input = rd_cmd.next();
				 switch (input) {
				  	case "s":
				  		if (commonUser.IsAdmin()) {
				  			System.out.println("Choose which user to show its statistic");
				  		} else {
				  			try {
					  			Scanner file_scan = new Scanner(new FileReader("statistics/" + commonUser.getName()));
								while(file_scan.hasNext()){	
								String f_out=file_scan.nextLine();
								System.out.println(f_out);
								}
							} catch(Exception e) {
								System.out.println("You have no statistics yet");
								continue;
								}
				  			}
				  		System.out.println();
				  		
					  	continue;
				  	case "t":
				  		logger.logStartTest(commonUser);
				  		testProcessing();
				  		logger.logTestFinished(commonUser);
					  	continue;
				  	case "logout":
				  		logger.logLogout(commonUser);
				  		break;
				  	case "e":
				  		logger.logLogout(commonUser);
				  		exit();
					  	break;
				 }
				 break;
			} 
		}
	}
	
	private static void exit() {
		System.out.println("Have a nice day!");
	  	System.exit(0);
	}
	
	private static void testProcessing() throws IOException {
		InOutManager inOut = new InOutManager(System.in, System.out);
		Test myPrivateTest = new Test(inOut);
		QuestionsMaker teacher = new QuestionsMaker();
		myPrivateTest.loadQuestions(teacher.getQuestionsForTest(QUESTIONS_FOR_TEST));
		List<Result> testResults;
		if  (myPrivateTest.isReadyToTest()) {
			testResults = myPrivateTest.conductTheTest();
			processResults(testResults);
			System.out.println();
		}
	}
	private static void processResults(List<Result> resultsOfTheTest) throws IOException {
		double wholeResults = 0;
		for(Result commonResult : resultsOfTheTest ) {
			wholeResults += commonResult.toDouble();
		}
		writeStatistics(wholeResults);
		printResultToPromt(wholeResults);
	}
	
	private static void writeStatistics(double wholeResults) throws IOException {
		FileWriter wr = new FileWriter("statistics/" + commonUser.getName(),true);
		Calendar cal = new GregorianCalendar();
		wr.write(new SimpleDateFormat().format(cal.getTime()));
		wr.write(" ");
		wr.write("test finished; ");
		wr.write("score is ");
		wr.write(String.valueOf(wholeResults));
		wr.write(" of " + QUESTIONS_FOR_TEST);
		wr.write("\r\n");
		wr.flush();
	}

	private static void printResultToPromt(double result) {
		System.out.printf("Your result is" + "\n" + result + " of " + QUESTIONS_FOR_TEST  + "\n");
	}
}
